package com.interview.item.javabase.arrays;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author:zxh
 * @Description: 集合中装入有对象，进行分组
 * @Date: 2020/5/31
 * @Modified By:
 */
public class ListGroupBy {

    /**
     * 根据对象User中的cityCode分组并且统计组的数量
     * @param args
     */
    public static void main(String[] args) {
        ArrayList<String> cityCode = new ArrayList<>();
        List<User> userList = getUserList();
        Map<String, Long> collect = userList.stream().collect(Collectors.groupingBy(User::getCityCode, Collectors.counting()));
        Iterator<Map.Entry<String, Long>> iterator = collect.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Long> next = iterator.next();
            if (next.getValue() == 1L) {
                cityCode.add(next.getKey());
            }
        }
        System.out.println(cityCode);
        System.out.println(collect);

    }


    private static List<User> getUserList(){
        ArrayList<User> arrayList = new ArrayList<>();
        User user = new User();
        user.setCityCode("123");
        user.setPriority("one");
        arrayList.add(user);

        User userO = new User();
        userO.setCityCode("123");
        userO.setPriority("one2");
        arrayList.add(userO);

        User userT = new User();
        userT.setCityCode("1234");
        userT.setPriority("one4");
        arrayList.add(userT);

        User userTh = new User();
        userTh.setCityCode("12345");
        userTh.setPriority("one45");
        arrayList.add(userTh);

        User userThr = new User();
        userThr.setCityCode("12345");
        userThr.setPriority("one45");
        arrayList.add(userThr);

        User userOne = new User();
        userOne.setCityCode("12354");
        userOne.setPriority("one54");
        arrayList.add(userOne);

        return arrayList;
    }
}
